package com.memory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.common.R;
import com.memory.daomain.Employee;
import com.memory.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author 邓哈哈
 * 2023/1/11 18:49
 * Function:登录登出与员工管理
 * Version 1.0
 */

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 测试
     * @return
     */
//    @GetMapping
//    public String getAll() {
//        log.info("EmployeeController正在运行...");
//        return "Fuck!";
//    }

    /**
     * 管理员登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("登陆系统正在运行...");
        //1.将页面提交的密码password进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //3.如果没有查询到则返回登录失败结果
        if (emp == null)
            return R.error("该用户名不存在");

        //4.密码对比,如果不一致则返回登录失败结果
        if (!password.equals(emp.getPassword()))
            return R.error("密码错误,请重新输入");

        //5.查看员工状态,如果为已禁用状态,则返回员工已禁用结果
        if (emp.getStatus() == 0)
            return R.error("该用户已被禁用");

        //6.登录成功,将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        log.info("员工id:{}", emp.getId());
        return R.success(emp);
    }

    /**
     * 管理员退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前员工的id(对应登录操作的第6步)
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
//        log.info("新增员工的信息:{}",employee.toString());
        //1.设置初始密码,需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2.设置创建时间,更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //3.设置创建人,修改人
//        Long empId = (Long) request.getSession().getAttribute("employee");//获取当前登录员工id
//
//        employee.setCreateUser(empId);//设置创建人
//        employee.setUpdateUser(empId);//修改人

        boolean save = employeeService.save(employee);

        return save ? R.success("新增员工成功") : R.error("新增员工失败");
    }

    /**
     * 修改员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        //设置修改人,修改日期
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
        //执行修改操作
        boolean update = employeeService.updateById(employee);

        return update ? R.success("账号状态更改成功！") : R.error("修改失败");
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee emp = employeeService.getById(id);

        return (emp != null) ? R.success(emp) : R.error("回显失败");
    }

    /**
     * 分页查询
     * 条件查询
     *
     * @param page     当前页码
     * @param pageSize 每页记录数
     * @param name     搜索条件
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(Integer page, Integer pageSize, String name) {
        Page pageInfo = employeeService.getPage(page, pageSize, name);

        return (pageInfo != null) ? R.success(pageInfo) : R.error("页面显示失败");
    }

}
